﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace employee.Controls
{
    public partial class emLeave : common.controls.baseListEdit
    {
        private data.baseDS.emLeaveDataTable myDataTbl = new data.baseDS.emLeaveDataTable();
        public emLeave()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception er)
            {
                ErrorHandler(this, er);;
            }            
        }

        public override event common.myEventHandler myOnError
        {
            add
            {
                base.myOnError += value;
                emLeaveDetail.myOnError += value;
            }
            remove
            {
                base.myOnError -= value;
            emLeaveDetail.myOnError -= value;
            }
        }
        public override void Init()
        {
            base.Init();
            this.myDataTbl.Clear();
            this.gridDataSource.DataSource = this.myDataTbl;

            emLeaveDetail.Init();
            SetData(this.myDataTbl);

            DataAccess.DataCache.LoadLeaveType(false);
            DataAccess.DataLibs.LoadData(DataAccess.DataCache.myBaseDS.emLeaveType);
            leaveTypeSource.DataSource = DataAccess.DataCache.myBaseDS.emLeaveType;
        }
        public override void LoadData(string emCode) 
        {
            myDataTbl.Clear();
            if (emCode != null)
            {
                DataAccess.DataLibs.LoadData(myDataTbl, emCode);
                emLeaveDetail.ReLoad();
            }
        }
        public override void SaveData(string emCode)
        {
            gridDataSource.EndEdit();
            for (int idx = 0; idx < myDataTbl.Count; idx++)
            {
                if (myDataTbl[idx].RowState == DataRowState.Deleted) continue;
                myDataTbl[idx].emCode = emCode;
            }
            DataAccess.DataLibs.UpdateData(myDataTbl);
        }
        public override void LockEdit(bool state)
        {
            base.LockEdit(state);
            emLeaveDetail.LockEdit(state);
        }
        public override bool CheckData()
        {
            return emLeaveDetail.CheckData();
        }
        public override void SetFocus()
        {
            emLeaveDetail.SetFocus();
        }
        public override void AddNew()
        {
            data.baseDS.emLeaveRow row;
            gridDataSource.AddNew();
            if (this.gridDataSource.Current != null)
            {
                row = (data.baseDS.emLeaveRow)((DataRowView)this.gridDataSource.Current).Row;
                DataAccess.DataLibs.InitData(row);
            }
            emLeaveDetail.SetDataSource(gridDataSource);
        }

        private void SetData(data.baseDS.emLeaveDataTable tbl)
        {
            emLeaveDetail.SetDataSource(gridDataSource);
            DataGridViewComboBoxColumn leaveTypeColumn = new DataGridViewComboBoxColumn();
            DataGridViewTextBoxColumn frDateColumn = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn durationColumn = new DataGridViewTextBoxColumn();
            this.detailGrid.Columns.AddRange(new DataGridViewColumn[] 
                        { frDateColumn, durationColumn, leaveTypeColumn }
                        );

            // 
            // frDateColumn
            // 
            frDateColumn.DataPropertyName = tbl.frDateColumn.ColumnName;
            frDateColumn.HeaderText = "Bắt.đầu";
            frDateColumn.Name = tbl.frDateColumn.ColumnName;
            frDateColumn.ReadOnly = true;
            frDateColumn.Width = 75;

            // 
            // leaveTypeColumn
            // 
            leaveTypeColumn.DataPropertyName = tbl.typeColumn.ColumnName;
            leaveTypeColumn.DataSource = leaveTypeSource;
            leaveTypeColumn.DisplayMember = DataAccess.DataCache.myBaseDS.emLeaveType.descriptionColumn.ColumnName;
            leaveTypeColumn.ValueMember = DataAccess.DataCache.myBaseDS.emLeaveType.codeColumn.ColumnName;
            leaveTypeColumn.HeaderText = "Lý do";
            leaveTypeColumn.Name = tbl.typeColumn.ColumnName;
            leaveTypeColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            leaveTypeColumn.ReadOnly = true;

            // 
            // durationColumn
            // 
            durationColumn.DataPropertyName = tbl.durationColumn.ColumnName;
            durationColumn.HeaderText = "Số.ngày";
            durationColumn.Width =65;
            durationColumn.Name = tbl.durationColumn.ColumnName;
            durationColumn.ReadOnly = true;

            detailGrid.myAutoFitColumn = leaveTypeColumn.Name;
        }
    }
}
